Repository navigation
craft-xls: read RC4 and RC4 CryptoAPI encrypted workbooks (ports of gridcraft#847, #3713) - #1018
Merged
Merged
Conversation
…t formats share craft-xls carried its own CellRef, Range, Value, CellError, Visibility, sheet-name quoting, built-in number format table and indexed palette. craft-xlsx and craft-ods need the same ones, so they move to a crate with no dependencies and craft-xls re-exports them: an application gets one Value and one CellRef whichever file it read. Added on the way: A1 parsing (CellRef::parse, Range::parse), Range::count and a1, CellError::parse and the newer error values SpreadsheetML stores as text, builtin_format_id for writers.
Each Dv after a DVal becomes a DataValidation in SpreadsheetML's terms: type, operator, formulas as A1 text from the top left cell of all the rule's ranges (a literal list quoted, items separated by commas), the four messages, alert style and flags. Exactly the DVal's count of rules is read, as Excel does; a rule Excel would refuse is skipped with a warning, and the rest are kept. A sheet keeps at most 65,536 rules (the count was not capped on its own). Ported from storytold/gridcraft#3636.
…ilter range - Notes with their TxO text, author and visibility. - CondFmt and CF records (cell value is, formula is), formulas from the bounding range's top left cell, and the DXFN as a DifferentialFormat whose unset parts are None. - The drawing group's BLIP store (PNG, JPEG, EMF, WMF with its placeable header, DIB as BMP) and each sheet's picture shapes with their client anchors. - The AutoFilter range from AutoFilterInfo and _FilterDatabase. From the review of the source PR: picture bytes are shared by the shapes that show them, pictures, comments and rules are capped per sheet, inflated metafiles share one allowance, damaged object and CF records are skipped with a warning instead of failing the read, and CF formulas are relative to the CondFmt's bounding range. ValidationOperator is now Operator, shared with conditional formats. Ported from storytold/gridcraft#3457.
A FilePass record with RC4 (version 1.1) is checked against the password through its MD5 verifier, and the record data after it is decrypted, rekeyed every 1024 bytes of the stream; record headers and the few records Excel leaves in the clear are kept. read_with_password takes the password. Without one, Excel's default password (VelvetSweatshop) is tried, as Excel opens a workbook protected only against changes, and Error::Encrypted says a password is needed. XOR obfuscation and RC4 CryptoAPI are Error::UnsupportedEncryption; a wrong password is Error::WrongPassword. Ported from storytold/gridcraft#847.
What current Excel writes for an .xls saved with a password (FilePass version 2.2, 3.2 or 4.2): H0 = SHA-1(salt, UTF-16LE password), each 1024-byte block keyed by SHA-1(H0, block number) cut to the key size, a 40-bit key padded with zeros to 128 bits, and the password checked against the verifier's SHA-1. Every size in the FilePass record is checked before use; CryptoAPI with AES, another cipher or hash, an external provider or a key outside 40 to 128 bits is named as unsupported. Ported from storytold/gridcraft#3713.
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 20 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
| let pw: Vec<u8> = password.encode_utf16().flat_map(u16::to_le_bytes).collect(); | ||
| let h0 = md5(&[&pw]); | ||
| let truncated = h0.get(..5).unwrap_or_default(); | ||
| let mut m = Md5::new(); |
| } | ||
|
|
||
| fn md5(parts: &[&[u8]]) -> [u8; 16] { | ||
| let mut m = Md5::new(); |
| } | ||
|
|
||
| fn sha1(parts: &[&[u8]]) -> [u8; SHA1_LEN] { | ||
| let mut m = Sha1::new(); |
| let verifier: [u8; 16] = std::array::from_fn(|n| n as u8 + 0x60); | ||
| let pw: Vec<u8> = password.encode_utf16().flat_map(u16::to_le_bytes).collect(); | ||
| let h0 = md5(&[&pw]); | ||
| let mut m = Md5::new(); |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Stacked on #1005 (on #999, on #956). Review the last two commits only.
Ported from storytold/gridcraft#847 by @itrycodinganditsbad (RC4, commit author Fabio Lussi) and storytold/gridcraft#3713 by @giuseppeps (RC4 CryptoAPI, which builds on #847; commit author giuseppeps).
What
craft-xlsreads password-encrypted workbooks ([MS-XLS] 2.2.10, [MS-OFFCRYPTO] 2.3.5, 2.3.6):read_with_password(bytes, limits, password)andis_encrypted(bytes). A workbook with no password reads the same whatever password is given.Error::WrongPasswordandError::UnsupportedEncryption(name)(XOR obfuscation; CryptoAPI with AES, another cipher or hash, an external provider, or a key outside 40 to 128 bits). A damaged FilePass isError::Invalid. Every size in it is checked before use.craft_xls::testing::{encrypt_rc4, encrypt_cryptoapi, cryptoapi_filepass}build encrypted fixtures.Where it lives, and what changed from the GridCraft PRs
craft-xls, notcraft-offcrypto. The scheme here is BIFF8's record-level stream encryption: which records stay clear, and block numbers by stream offset.craft-offcryptohandles encrypted OOXML packages (EncryptionInfo/EncryptedPackage), which this shares no code with. If another binary Office format needs RC4 CryptoAPI later, the key derivation can move then.VelvetSweatshop) is now tried byread/read_withitself, as Excel does, instead of by GridCraft's engine. SoError::Encryptedmeans a password is really needed, and every app gets the same behaviour.Error::Invalid. Before craft-dxf: R12 binary, more code pages, borrowing sections, exponent reals (ports of open CadCraft PRs) #847 every FilePass wasError::Encrypted. One existing test was updated for this.unwrapor indexing, sincecraft_xls::testingis always compiled here.finish_open) stay in GridCraft.Checked (own target dir)
cargo fmt --check -p craft-xls,cargo clippy -p craft-xls --all-targets -- -D warnings: clean, at both commits.cargo test -p craft-xls: all pass, at both commits (tests/read.rs: 21 after the RC4 commit, 23 after CryptoAPI). Ported tests:rc4cryptoapi_password.xls, Apache POI's35897-type4.xlsandpassword.xls). I did not re-run that here, and no third-party file is committed.The format part of gridcraft#847 and #3713 is superseded by this. What remains in GridCraft is the password prompt in the engine, which calls
craft_xls::read_with_password, and its docs.